﻿@using System.Web.Optimization
@using MultiTenant.Helpers
@model MultiTenant.Models.BaseViewModel
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/layout.bundle.css")
    @Scripts.Render("~/Scripts/layout.bundle.javascript")
</head>
<body>
    <header>
    </header>
    
    <div id="tenantsParent">
        <div id="tenants"></div>
    </div>

    <nav>
        @if (@Model.CurrentTenant != null)
        {
           @Html.SiteMenu(@Model.CurrentTenant) 
        }
    </nav>

    @RenderBody()
    
    @RenderSection("scripts", required: false)
    
    <script type="text/javascript">

        (function () {
            
            function setTenant(id) {
                $.ajax({
                    success: function () {
                        location.reload();
                    },
                    type: 'PUT',
                    url: '@Url.Action("Put", "api/TenantApi", new { Area = ""})' + '?id=' + id.toString()
                });
            }

            $('#menu').kendoMenu({
            });
            $('#tenants').kendoDropDownList({
                change: function() {
                    setTenant(this.value().toString());
                },
                dataTextField: "Name",
                dataValueField: "Id",
                dataSource: {
                    transport: {
                        read: {
                            type: 'GET',
                            url: '@Url.Action("GetAll", "api/TenantApi", new { Area = ""})'
                        }
                    }
                }
            });
            $.ajax({
                success: function (result) {
                    if (!result) {
                        setTenant(1);
                    }
                    $('#tenants').data('kendoDropDownList').value(result.Id);
                },
                type: 'GET',
                url: '@Url.Action("Get", "api/TenantApi", new { Area = ""})'
            });
        })()    

    </script>

</body>
</html>
